﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Orid.Data.SqlServer;
using Orid.Management.Statistics.Tools.Decomposition.Model;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class UploadJobStore :DataBase
    {
        #region 构造函数 sql 内容

        protected StringBuilder sqlText = new StringBuilder() ;

        public UploadJobStore()
        {   //JobFile
            sqlText.Append(" SELECT [JobID], [UploadAccountID], [JobFile], [JobSize], [UploadDateTime], [UpdateDateTime], [EntryDuration] FROM @UploadJobStoreTableName with(NOLOCK)");//ReadPast
            sqlText.Append(" WHERE JobID=@JobID ");
            sqlText.Append(" AND  UploadAccountID=@AccountID "); 
        }
        #endregion

        public virtual ModelUploadJobStore GetEntryDuration(Guid? jobID, int? accountID) { return null; }

        protected ModelUploadJobStore GetEntryDuration(Guid? jobID, int? accountID, string connectString, string tableName)
        {
            try
            {
                ModelUploadJobStore modelUploadJobStore = new ModelUploadJobStore();

                if (accountID == null)
                {
                    return null;
                }

                sqlText.Replace("@UploadJobStoreTableName", tableName);

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();
                    System.Data.SqlClient.SqlParameter[] SqlParameter;
                    SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {    
                                       new System.Data.SqlClient.SqlParameter("@JobID",System.Data.SqlDbType.UniqueIdentifier),  
                                       new System.Data.SqlClient.SqlParameter("@AccountID",System.Data.SqlDbType.Int), 

                                 };

                    SqlParameter[0].Value = jobID;
                    SqlParameter[1].Value = accountID;


                    SqlDataReader reader= SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter) ;


                    while (reader.Read())
                    {
                        //Byte[] jobFile = (Byte[])reader["JobFile"];
                        //job = new Orid.Future.Job.Serializer<Orid.Future.Job.Job>().DeSerialize(jobFile);

                        if (reader["UploadDateTime"] != DBNull.Value)
                            modelUploadJobStore.UploadDateTime = (DateTime)reader["UploadDateTime"];
                        if (reader["EntryDuration"] != DBNull.Value)
                            modelUploadJobStore.EntryDuration = Convert.ToDouble(reader["EntryDuration"]);
                    } 

                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();

                    return modelUploadJobStore;
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }
        }

        public virtual void GetFGEntryResult  (Guid? jobID, int? accountID, ref ModelFrameResult frameResult) { return ; }


        protected void GetFGEntryResult(Guid? jobID, int? accountID, string connectString, string tableName,ref ModelFrameResult frameResult)
        {
            try
            {               

                if (accountID == null)
                {
                    return  ;
                }

                sqlText.Replace("@UploadJobStoreTableName", tableName);

                using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectString))
                {
                    conn.Open();
                    System.Data.SqlClient.SqlParameter[] SqlParameter;
                    SqlParameter = new System.Data.SqlClient.SqlParameter[]
                                 {    
                                       new System.Data.SqlClient.SqlParameter("@JobID",System.Data.SqlDbType.UniqueIdentifier),  
                                       new System.Data.SqlClient.SqlParameter("@AccountID",System.Data.SqlDbType.Int), 

                                 };

                    SqlParameter[0].Value = jobID;
                    SqlParameter[1].Value = accountID;


                    SqlDataReader reader = SqlHelper.ExecuteReader(conn, System.Data.CommandType.Text, sqlText.ToString(), SqlParameter);


                    while (reader.Read())
                    {
                      

                        if (reader["JobFile"] != DBNull.Value)
                        {
                            Byte[] jobFile = (Byte[])reader["JobFile"];
                            Orid.Management.Common.Job.Job entryJob = new Orid.Management.Common.Job.Serializer<Orid.Management.Common.Job.Job>().DeSerialize(jobFile);

                            bool bFlag = GetFGResult(entryJob, ref  frameResult);
                            
                            if (bFlag)
                            {
                                break ;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        
                    }

                    if (conn.State == System.Data.ConnectionState.Open)
                        conn.Close();

                   
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw (new Exception(ex.Message));
            }
        }

        private bool  GetFGResult( Orid.Management.Common.Job.Job entryJob, ref ModelFrameResult frameResult)
        {
            bool bFlag = false;

            List<ModelFrameResult> frameResultList = new List<ModelFrameResult>(); 

            if (entryJob.JobClassID == 16)
            { 
                frameResult.FJobID = entryJob.JobID;
                frameResult.FLoginName = new AccountOnline().GetLoginName( entryJob.AccountID );
                frameResult.FDuration = entryJob.Duration;
            
                //frameResult.FInputDateTime = entryJob.
            }
            else if (entryJob.JobClassID == 17)
            {
                frameResult.GJobID = entryJob.JobID;
                frameResult.GLoginName = new AccountOnline().GetLoginName(entryJob.AccountID);
                frameResult.GDuration = entryJob.Duration;
            }
            
            foreach (Orid.Management.Common.Job.JobItem jobItem in entryJob.Items)
	        {
                if (jobItem != null)
                {
                    //如果结果是这个画像的结果
                    if (jobItem.ImageID == frameResult.FrameResultID)
                    {
                       

                        frameResult.FrameResultID = jobItem.ImageID;
                   

                        if (jobItem.InputResult is Orid.Management.Common.Job.CheckDataResult)
                        {
                             
                            Orid.Management.Common.Job.CheckResultContent checkResultContent = (jobItem.InputResult as Orid.Management.Common.Job.CheckDataResult).Content as Orid.Management.Common.Job.CheckResultContent;


                            foreach ( Orid.Management.Common.Job.ResultObject resultObject in checkResultContent.Results)
                            {
                                ModelFrameResult.ModelFrameDetailReuslt detailResult = new ModelFrameResult.ModelFrameDetailReuslt ();
                                 if (entryJob.JobClassID == 16)
                                 {
                                     detailResult.JobClass = Orid.Management.Statistics.Tools.Decomposition.Logic.JobClassAll.F;
                                     frameResult.FComment = jobItem.InputResult.Comment.ToString();
                                     frameResult.FInputDateTime = jobItem.InputResult.InputDateTime;

                                 }
                                 else if (entryJob.JobClassID == 17)
                                 {
                                     detailResult.JobClass = Orid.Management.Statistics.Tools.Decomposition.Logic.JobClassAll.G;
                                     frameResult.GComment = jobItem.InputResult.Comment.ToString();
                                     frameResult.GInputDateTime = jobItem.InputResult.InputDateTime;
                                 }

                                detailResult.ImageType = resultObject.ResultItem.ImageType;
                                detailResult.IsChange = resultObject.IsChange;
                                detailResult.Rect = resultObject.Rect;
                                detailResult.Result = resultObject.ResultItem.ToString();

                                if (frameResult.resultItem == null)
                                    frameResult.resultItem = new List<ModelFrameResult.ModelFrameDetailReuslt>();

                                frameResult.resultItem.Add(detailResult);
                            }

                  
                        }

                        bFlag = true;
                    }
                }
               
              
        		 
	        }


            return bFlag;
         
 
        }

    }
}
